home *** CD-ROM | disk | FTP | other *** search
- #include "AcrobatAppleScript.h"
-
- #include "ASCalls.h"
- #include "AVCalls.h"
- #include "CorCalls.h"
- #include "PDCalls.h"
- #include "CosCalls.h"
-
- static pascal Boolean MyEventFilter( DialogPtr theDialog, EventRecord theEvent, DialogItemIndex itemHit )
- {
- Boolean handledIt;
-
- handledIt = false;
-
- if( theEvent.what == keyDown )
- {
- itemHit = 3;
- }
-
- return( handledIt );
- }
-
- Boolean AddModifyScriptDialog( char *theScript )
- {
-
- Boolean done;
- OSErr err;
- DialogPtr theEditorDialog;
- DialogItemIndex index;
- DialogItemType theType;
- Handle theHandle;
- Rect theBox;
- ModalFilterUPP theFilterProc;
-
- theFilterProc = NewModalFilterUPP( MyEventFilter );
-
- /* If I was paying attention to the API in Acrobat, this would be SafeGetNewDialog */
- theEditorDialog = GetNewDialog( 1024, nil, (WindowPtr)(-1));
-
- if( theEditorDialog != nil )
- {
-
- GetDialogItem( theEditorDialog, 3, &theType, &theHandle, &theBox );
- if( theHandle != nil )
- {
- setdialogitemtext( theHandle, theScript );
- }
-
- err = SetDialogDefaultItem( theEditorDialog, 1 );
-
- err = SetDialogCancelItem( theEditorDialog, 2 );
-
- done = false;
-
- do {
-
- ModalDialog( theFilterProc, &index );
-
- if( index == 1 )
- done = true;
-
- if( index == 2 )
- done = true;
-
- } while ( done == false );
-
- if( index == 1 )
- {
- getdialogitemtext( theHandle, theScript );
- }
-
- DisposeDialog( theEditorDialog );
- }
-
- DisposeModalFilterUPP( theFilterProc );
-
- if( index == 1 )
- return true;
-
- return false;
- }
-
- Boolean IsKeyDown( short whichKeyCode )
- {
- unsigned char km[16];
- GetKeys((unsigned long *) km );
- return(( km[whichKeyCode>>3]>>(whichKeyCode&7)) & 1 );
-
- }
- void GetScriptFromAnnot( PDAnnot annot, char *theScript )
- {
- char *scriptPtr;
- CosObj cAnnotObj, scriptObj, theScriptObj, nullObj;
- CosType scriptType;
- ASInt32 nBytes;
- ASAtom scriptArray;
-
- nullObj = CosNewNull( );
-
- /* get the annotation CosObj */
- cAnnotObj = PDAnnotGetCosObj(annot);
-
- if( CosDictKnown( cAnnotObj, ASAtomFromString( "AppleScript" )) == true )
- {
- scriptObj = CosDictGet( cAnnotObj, ASAtomFromString( "AppleScript" ));
-
- /* make sure that what comes out of the annot cos object isn't null */
- if( CosObjEqual( scriptObj, nullObj ) == false )
- {
- /* get the actual script out of the script CosObj */
- theScriptObj = CosDictGet( scriptObj, ASAtomFromString( "TheScript" ));
-
- if( CosObjEqual( theScriptObj, nullObj ) == false )
- {
- scriptType = CosObjGetType( theScriptObj );
-
- nBytes = 0;
- scriptPtr = CosStringValue( theScriptObj, &nBytes );
-
- strncpy( theScript, scriptPtr, ( nBytes < kScriptBufferSize ? nBytes : kScriptBufferSize ));
- }
- }
- }
- }
-
- void DoScript( char *theScript )
- {
- ComponentInstance scriptingComponent;
- AEDesc componentName, scriptText, resultText;
- OSAID scriptID, resultID;
- OSAError myOSAError, ignoreErr;
- OSErr err;
-
- scriptingComponent = OpenDefaultComponent( kOSAComponentType, kOSAGenericScriptingComponentSubtype );
- myOSAError = OSAScriptingComponentName( scriptingComponent, &componentName );
- if( myOSAError == noErr )
- {
- err = AECreateDesc( typeChar, theScript, strlen( theScript ), &scriptText );
- if( err == noErr )
- {
- scriptID = kOSANullScript;
- myOSAError = OSACompile( scriptingComponent, &scriptText, kOSAModeNull, &scriptID );
-
- if( myOSAError == noErr )
- {
- myOSAError = OSAExecute( scriptingComponent, scriptID, kOSANullScript, kOSAModeNull, &resultID );
- ignoreErr = OSADispose( scriptingComponent, scriptID );
-
- if( myOSAError != noErr )
- {
- AVAlert( ALERT_STOP, "Couldn't Execute Script", "OK", nil, nil, true );
- }
- } else {
- AVAlert( ALERT_STOP, "Couldn't Compile Script", "OK", nil, nil, true );
- }
-
- ignoreErr = AEDisposeDesc( &scriptText );
- }
- }
- }
-
-